package org.groovymud.shell.io.filters;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

public class TerminalFilterChain<T> {

	private List<TerminalIOFilter<T>> filters;

	public void register(TerminalIOFilter<T> filter) {
		getFilters().add(filter);
	}

	public boolean unregister(TerminalIOFilter<T> filter) {
		return getFilters().remove(filter);
	}

	public String doFilter(T metaData, String str, FilterType type) {
		List<TerminalIOFilter<T>> copy = new LinkedList<TerminalIOFilter<T>>(getFilters());
		Iterator<TerminalIOFilter<T>> iterator = copy.iterator();
		while (iterator.hasNext()) {
			TerminalIOFilter<T> filter = iterator.next();
			if (filter.getFilterType() == type) {
				str = filter.doFilter(metaData, str);
			}
		}
		return str;
	}

	public void setFilters(List<TerminalIOFilter<T>> filters) {
		this.filters = filters;
	}

	public List<TerminalIOFilter<T>> getFilters() {
		return filters;
	}
}
